package com.zk.window;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * 悬浮窗的制作
 * 
 * @author 锋翼
 * @link www.apkstory.com
 */
public class AService extends Service implements Runnable{

	WindowManager wm = null;
	WindowManager.LayoutParams wmParams = null;
	TextView view;
	private float mTouchStartX;
	private float mTouchStartY;
	private float x;
	private float y;
	
	private Handler h;
	@Override
	public void onCreate() {
		super.onCreate();
//		view = LayoutInflater.from(this).inflate(R.layout.main, null);
		view=new TextView(this);
		view.setText("hello");
		view.setText(String.valueOf(getAvailMemory()/1000000));
		new Thread(this).start();
		createView();
		
		h=new Handler(new Callback() {
			@Override
			public boolean handleMessage(Message msg) {
//				System.out.println(String.valueOf(Runtime.getRuntime().freeMemory()));
				view.setText(String.valueOf(getAvailMemory()/1000000));
				return false;
			}
		});
	}
	
	private void createView() {
		// 获取WindowManager
		wm = (WindowManager) getApplicationContext().getSystemService("window");
		// 设置LayoutParams(全局变量）相关参数
		wmParams = new WindowManager.LayoutParams();
		wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;// 该类型提供与用户交互，置于所有应用程序上方，但是在状态栏后面
		wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 不接受任何按键事件
		wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
		// 以屏幕左上角为原点，设置x、y初始值
		wmParams.x = 0;
		wmParams.y = 0;
		// 设置悬浮窗口长宽数据
		wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wmParams.format = PixelFormat.RGBA_8888;

		wm.addView(view, wmParams);

		view.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// 获取相对屏幕的坐标，即以屏幕左上角为原点
				x = event.getRawX();
				// 25是系统状态栏的高度,也可以通过方法得到准确的值，自己微调就是了
				y = event.getRawY() - 25;
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					// 获取相对View的坐标，即以此View左上角为原点
					mTouchStartX = event.getX();
					mTouchStartY = event.getY() + view.getHeight() / 2;
					break;
				case MotionEvent.ACTION_MOVE:
					updateViewPosition();
					break;
				case MotionEvent.ACTION_UP:
					updateViewPosition();
					mTouchStartX = mTouchStartY = 0;
					break;
				}
				return true;
			}

		});
	}

	private void updateViewPosition() {
		// 更新浮动窗口位置参数
		wmParams.x = (int) (x - mTouchStartX);
		wmParams.y = (int) (y - mTouchStartY);
		wm.updateViewLayout(view, wmParams);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(1000);
				h.sendMessage(new Message());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public long getAvailMemory() {  
		ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);  
		ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();  
		am.getMemoryInfo(mi);
		return mi.availMem;
	}



}
